import sys
from random import randint

from src.level import Level
from src.group import \
    (DisplayGroup,
     UpdateGroup,
     CollisionGroup,
     DamageGroup,
     EnemyAttackGroup,
     PlayerGroup,
     PlayerAttackGroup,
     EnemyGroup)
from manager import sprite_mgr
from settings import *
from src.static import ArmStatic, SkillStatic


class Game(object):
    def __init__(self):
        PG.display.set_caption('Sprout land')
        self.clock = PG.time.Clock()
        self.manager = sprite_mgr

    def setup(self):
        self._setup_group()
        self._setup_static()
        self._setup_level()

    def _setup_group(self):
        print("game setup group.....")

        self.manager.set_group(PGGroup.player.value, PlayerGroup())
        self.manager.set_group(PGGroup.display.value, DisplayGroup())
        self.manager.set_group(PGGroup.update.value, UpdateGroup())
        self.manager.set_group(PGGroup.enemy.value, EnemyGroup())
        self.manager.set_group(PGGroup.collision.value, CollisionGroup())
        self.manager.set_group(PGGroup.player_attack.value, PlayerAttackGroup())
        self.manager.set_group(PGGroup.enemy_attack.value, EnemyAttackGroup())
        self.manager.set_group(PGGroup.damage.value, DamageGroup())

    def _setup_static(self):
        print("game setup static.....")

        self.manager.set_static(PGStaticPos.arm.name, ArmStatic())
        self.manager.set_static(PGStaticPos.skill.name, SkillStatic())

    def _setup_level(self):
        p1 = self.manager.create_player(2, (200, 300), self.manager.get_group(PGGroup.update.value),
                                        self.manager.get_group(PGGroup.player.value))
        print("game setup level.....")

        self.manager.set_level(1, Level())
        self.level = self.manager.get_level(1)
        self.level.setup()

    def run(self):
        PG.key.stop_text_input()
        p2 = self.manager.create_enemy(2, (350, 200), self.manager.get_group(PGGroup.display.value),
                                       self.manager.get_group(PGGroup.update.value),
                                       self.manager.get_group(PGGroup.collision.value),
                                       self.manager.get_group(PGGroup.enemy.value))
        self.creat_enemy()

        while True:
            for event in PG.event.get():
                if event.type == PG.QUIT:
                    PG.quit()
                    sys.exit()
            dt = self.clock.tick() / 1000
            self.level.run(dt)
            PG.display.update()

    def creat_enemy(self):
        # 循环生成坦克
        for i in range(1):
            x = randint(1, 1280)  # 坦克出现的左侧位置
            y = randint(1, 800)  # 坦克出现的左侧位置
            self.manager.create_enemy(2, (x, y), self.manager.get_group(PGGroup.display.value),
                                      self.manager.get_group(PGGroup.update.value),
                                      self.manager.get_group(PGGroup.collision.value),
                                      self.manager.get_group(PGGroup.enemy.value))


if __name__ == '__main__':
    game = Game()
    game.setup()
    game.run()
